sycamore x futures
2022-03-29 ยท 1 min read
Can use gloo / gloo_timers crate for web-compatible futures. For example, gloo_timers::sleep(duration) -> TimeoutFuture.
Spawn a future on the current thread. Must be 'static and so can't contain any ScopeRefs or w/e:
sycamore::futures::spawn_local(future)
Spawn a !Send future (which can contain ScopeRefs) using ctx.spawn_future:
use sycamore::futures::ScopeSpawnFuture;
ctx.spawn_future(future);
Convert a Future<U> to a Signal<U> using ctx.create_resource:
use sycamore::futures::ScopeFuturesExt;
ctx.create_resource(future);
Web Workers #
Like threads for browser (but worse lol). No shared memory allowed; only serialized message passing. Workers can't manipulate the DOM.
SharedWorkers (also "Public" workers) are a special kind of worker that can be accessed from multiple browser windows, tabs, or other workers. There can only exist one instance at a time.- "Private" workers can be fired up and down at-will. Good for farming out work.
Communicating with workers:
- Bridges allow bidi comms b/w worker and component (or other workers).
- Dispatchers allow uni comms from component to worker.